// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UITickSizeRule.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge.UI.Forms
{
    #region Using Directives

    using System;
    using System.Drawing;
    using System.Linq;
    using System.Windows.Forms;

    using Org.OpenTrader.Framework.Events;
    using Org.OpenTrader.Framework.UI.Forms;

    #endregion

    /// <summary>
    /// The ui tick size rule.
    /// </summary>
    public partial class UITickSizeRule : UIControl
    {
        // TickSizeRule currently edited
        #region Constants and Fields

        /// <summary>
        /// The tsr.
        /// </summary>
        private TickSizeRule tsr;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="UITickSizeRule"/> class.
        /// </summary>
        public UITickSizeRule()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="UITickSizeRule"/> class.
        /// </summary>
        /// <param name="tsr">
        /// The tsr.
        /// </param>
        public UITickSizeRule(TickSizeRule tsr)
        {
            this.InitializeComponent();

            this.tsr = tsr;
            this.name.Text = string.Empty + tsr.Name;
        }

        #endregion

        #region Methods

        /// <summary>
        /// The ticksizeRulesAdd_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnTicksizeRulesAddClick(object sender, EventArgs e)
        {
            double price;
            double ticksize;
            double.TryParse(this.ticksizeRulesAddPrice.Text, out price);
            double.TryParse(this.ticksizeRulesAddTicksize.Text, out ticksize);

            lock (this.tsr.TickSizeTable)
            {
                this.tsr.TickSizeTable[price] = ticksize;
            }

            this.ticksizeRulesAddPrice.Text = string.Empty;
            this.ticksizeRulesAddTicksize.Text = string.Empty;

            // ticksizeRules.Refresh();
        }

        /// <summary>
        /// The ticksizeRulesAddPrice_ text changed.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnTicksizeRulesAddPriceTextChanged(object sender, EventArgs e)
        {
            this.ticksizeRulesAddPrice.BackColor = System.Drawing.SystemColors.Window;

            if (this.ticksizeRulesAddPrice.Text.Length <= 0)
            {
                goto clear;
            }

            double price;
            if (!double.TryParse(this.ticksizeRulesAddPrice.Text, out price))
            {
                this.ticksizeRulesAddPrice.BackColor = Color.LightSalmon;
                goto clear;
            }

            if (price < 0)
            {
                goto clear;
            }

            if (this.tsr.TickSizeTable.ContainsKey(price))
            {
                goto clear;
            }

            // Filtering done
            goto enable;

            enable:
            this.ticksizeRulesAddTicksize.Enabled = true;
            return;

            clear:
            this.ticksizeRulesAddTicksize.Text = string.Empty;
            this.ticksizeRulesAddTicksize.Enabled = false;
            this.ticksizeRulesAdd.Enabled = false;
            return;
        }

        /// <summary>
        /// The ticksizeRulesAddTicksize_ text changed.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnTicksizeRulesAddTicksizeTextChanged(object sender, EventArgs e)
        {
            this.ticksizeRulesAddTicksize.BackColor = System.Drawing.SystemColors.Window;

            if (this.ticksizeRulesAddTicksize.Text.Length <= 0)
            {
                goto disable;
            }

            double ticksize;
            if (!double.TryParse(this.ticksizeRulesAddTicksize.Text, out ticksize))
            {
                this.ticksizeRulesAddTicksize.BackColor = Color.LightSalmon;
                goto disable;
            }

            if (ticksize <= 0 || ticksize >= 1)
            {
                goto disable;
            }

            // Filtering done
            goto enable;

            enable:
            this.ticksizeRulesAdd.Enabled = true;
            return;

            disable:
            this.ticksizeRulesAdd.Enabled = false;
            return;
        }

        /// <summary>
        /// The ticksizeRules_ cell click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnTicksizeRulesCellClick(object sender, DataGridViewCellEventArgs e)
        {
            // Remove
            if (e.ColumnIndex == 2 && e.RowIndex >= 0)
            {
                try
                {
                    lock (this.tsr.TickSizeTable)
                    {
                        var tsrPrice = this.tsr.TickSizeTable.Keys.ToArray()[e.RowIndex];
                        var tsrValue = this.tsr.TickSizeTable[tsrPrice];

                        this.tsr.TickSizeTable.Remove(tsrPrice);
                    }
                }
                catch
                {
                }
            }
        }

        /// <summary>
        /// The ticksizeRules_ ui grid query cell info.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnTicksizeRulesUIGridQueryCellInfo(object sender, VirtualGridEventArgs.QueryCellInfo e)
        {
            if (e.RowIndex >= this.tsr.TickSizeTable.Keys.Count() || e.RowIndex >= this.tsr.TickSizeTable.Values.Count())
            {
                return;
            }

            double tsrPrice;
            double tsrValue;

            tsrPrice = this.tsr.TickSizeTable.Keys.ElementAt(e.RowIndex);
            tsrValue = this.tsr.TickSizeTable.Values.ElementAt(e.RowIndex);

            switch (e.ColumnIndex)
            {
                case 0:
                    e.Value = tsrPrice;
                    break;

                case 1:
                    e.Value = tsrValue;
                    break;

                case 2:
                    e.Value = "Remove";
                    (this.ticksizeRules[e.ColumnIndex, e.RowIndex] as DataGridViewDisableButtonCell).Enabled = true;

                    // e.RowIndex != 0; // don't remove first line
                    break;
            }
        }

        /// <summary>
        /// The ticksizeRules_ ui grid query row count.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnTicksizeRulesUIGridQueryRowCount(object sender, VirtualGridEventArgs.QueryRowCount e)
        {
            e.RowCount = this.tsr.TickSizeTable.Count;
        }

        #endregion
    }
}